# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

from scrapy.http import Request

from scrapy.pipelines.files import FilesPipeline


class ComicspiderPipeline(FilesPipeline):
    def get_media_requests(self, item, info):
        # print("="*60,"get_media_requests")
        return [Request(x, headers={"Referer": item["referer"]},
                        meta={"file_name": item["file_name"], "book": item["book"], "chapter": item["chapter"]})
                for x in item.get("file_urls", [])]

    # 如果出错，并不会抛异常信息，所以没有执行可能是函数里有错
    # 会根据setting里设置的FILES_EXPIRES比对文件时间，没过期不会重新下载，默认90天
    def file_path(self, request, response=None, info=None):
        # print(=" * 60, request.meta)
        path = "{}/{}/{}".format(request.meta["book"], request.meta["chapter"], request.meta["file_name"])
        # print("="*60, path)
        return path

